var _lat;
var _lng;
var _city;
$(function() {

});
function getCityChexing() {
	_lat = $("#hiddenmLat").val();
	_lng = $("#hiddenmLng").val();
	if (_lat && _lng) {
		getLocation(_lat, _lng);
	} else {
		alert("门店经纬度获取失败！");
	}
}
function getLocation(lat, lng) {
	/*
	$.ajax( {
		--url : "../index/getLocationByGgLatlng.do?lat="+lat+"&lng="+lng+"&t=" + (new Date()),
        url : "http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&language=zh-CN&sensor=true",
		dataType : "json",
		type : 'get',
		async:false,
		success : function(data) {
			if (data.results.length > 0) {
				var first=data.results[0];
					var addr=first.formatted_address;
		            $('.address').val(addr);
		            var address_components=first.address_components;
					$("#hiddenProvince").val("");
					$("#hiddenCity").val("");
					$("#hiddenTown").val("");
					for(k=0;k<address_components.length;k++){
						var p1=address_components[k].types[0];
						var p2=address_components[k].types[1];
						if("administrative_area_level_1"==p1&&"political"==p2){
							$("#hiddenProvince").val(address_components[k].short_name);
						}else if("locality"==p1&&"political"==p2){
							$("#hiddenCity").val(address_components[k].short_name);
		$('#city').html(address_components[k].short_name);
		_city=address_components[k].short_name;
						}else if("sublocality"==p1&&"political"==p2){
							$("#hiddenTown").val(address_components[k].short_name);
						}
					}
		getCheXingByCity(_city);
	} else {
		alert("定位失败");
	}
}
	});
		$.ajax( {
			url : "../index/getLocation.do?t=" + (new Date()),
			dataType : "json",
		type : 'post',
			data : {
				lat : lat,
				lng : lng
			},
		success : function(data) {
					if (data.status == 0) {
						_city = data.result.addressComponent.city;//将电池的城市赋值
						$('#hiddenProvince').val(data.result.addressComponent.province);
						$('#hiddenCity').val(_city);
					var addr=data.result.formatted_address;
						//addr=addr.replace('中国','');
						$('.address').val(addr);
						$('#hiddenTown').val(data.result.addressComponent.district);
						getCheXingByCity(_city);
			        } else {alert("定位失败");}
		     }
		});
		*/
		$.ajax( {
			url : "../index/getLocationByGaodeLatlng.do?lng="+lng+"&lat="+lat+"&t=" + (new Date()),
			dataType : "json",
		type : 'get',
		success : function(data) {
					if (data.status =="1") {
						var first=data.regeocode;
					    var addr=first.formattedAddress;
					    
					    var address_components=first.addressComponent;
					$("#hiddenProvince").val(address_components.province);
					if(address_components.city.length==0){
						_city = address_components.province;//将电池的城市赋值
					}else{
						$("#m_city").val(address_components.city);
						_city = address_components.city;//将电池的城市赋值
					}
					$("#hiddenTown").val(address_components.district);
					$('#hiddenCity').val(_city);
						getCheXingByCity(_city);
			        } else {alert("定位失败");}
		     }
		});
}
function getCheXingByCity(city) {
	if (city) {
		$.ajax( {
					url : 'ajaxQueryChexByCity.do?t=' + (new Date()),
					type : 'post',
					data : {
						'city' : city
					},
					dataType : 'json',
					success : function(data) {
						if (data.length > 0) {
							var html = "";
							for ( var i = 0; i < data.length; i = i + 1) {
								var timage = '${pageContext.request.contextPath}/img/category.jpg';
								var bg = 'i-bg-red';
								var j = i % 4;
								var k = i % 2;
								if (j == 1) {
									bg = 'i-bg-blue';
								}
								if (j == 2) {
									bg = 'i-bg-purple';
								}
								if (j == 3) {
									bg = 'i-bg-green';
								}
								html += '<div class="module">';
								html += '<section class="i-banner"  onclick="queryDianchi('+ data[i].id+ ',\''+ data[i].name+ '\');">';
								if (0 == k) {
									html += '	<div class="i-banner-title ' + bg + '">';
									html += '		<span></span><span></span><h3>' + data[i].name + '</h3>';
									html += '	</div>';
									html += '	<div class="i-banner-pic">';
									if (data[i].imageUrl) {
										timage = data[i].imageUrl;
									}
									html += '	<img   src="../bettery/img/loading.gif" xSrc="' + timage + '">';
									html += '	</div>';
								} else {
									html += '	<div class="i-banner-pic">';
									if (data[i].imageUrl) {
										timage = data[i].imageUrl;
									}
									html += '	<img   src="../bettery/img/loading.gif"  xSrc="' + timage + '">';
									html += '	</div>';
									html += '	<div class="i-banner-title ' + bg + '">';
									html += '		<label ></label><label ></label><h3>' + data[i].name + '</h3>';
									html += '	</div>';
								}
								html += '</section>';
								html += '</div>';
							}
							hideLoading();
							$('#category-list').html(html);
							scrollLoad();

						} else {
							alert('加载车型失败：' + data.message);
						}
					}
				});
	} else {
		alert('不能确定当前城市');
	}
}
function queryDianchi(id, name) {
	if (id) {
		$("#tsss").val(new Date());
		$("#chexingId").val(id);
		$("#hiddenChexingName").val(name);
		$("#dingDanForm1").submit();
	}

}